﻿// Necroz project team © <2015> <ArchLord2EMU>
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Author:Sagara
// Date:2015-09-06-23:29

using System;
using System.Net;
using CM.Framework.Network;

namespace ArchLord2Commons.Network
{
   public class ArchLordServer<TALClient> : IDisposable 
        where TALClient : AALClient
    {
        private readonly IAALFrameHandler _frameHandler;
        private readonly TCPServerService<TALClient> _clientsHost;

        public ArchLordServer(IAALFrameHandler frameHandler, int maxConnections = 5000)
        {
            _frameHandler = frameHandler;
            _clientsHost = new TCPServerService<TALClient>(maxConnections);
        } 

        /// <summary>
        /// Start listening for new clients
        /// </summary>
        /// <param name="host">ip</param>
        /// <param name="port">port</param>
        /// <param name="listenBacklog">pending for acception queue maximal size</param>
        public void StartListening(string host, int port, int listenBacklog = 100)
        {
            _clientsHost.StartListening(host, port, connection => connection.OnClientConnected(_frameHandler), listenBacklog);
        }

        /// <summary>
        /// Start listening for new clients
        /// </summary>
        /// <param name="endPoint">listen endpoint</param>
        /// <param name="listenBacklog">pending for acception queue maximal size</param>
        public void StartListening(EndPoint endPoint, int listenBacklog = 100)
        {
            _clientsHost.StartListening(endPoint, connection => connection.OnClientConnected(_frameHandler), listenBacklog);
        }

        public void Dispose()
        {
            _clientsHost.Dispose();
        }
    }
}
